package com.smooth.site.sx.mag.biz.compcollege.comp.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smooth.dao.gen.BaseCollegeMapper;
import com.smooth.dao.gen.BizCompCollegeMapper;
import com.smooth.dao.gen.BizCompanyMapper;
import com.smooth.dao.gen.bean.BaseCollege;
import com.smooth.dao.gen.bean.BizCompCollege;
import com.smooth.dao.gen.bean.BizCompCollegeExample;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.genx.GxCollegeMapper;
import com.smooth.site.sx.mag.biz.compcollege.comp.bean.CompCollegeBean;
import com.smooth.site.sx.mag.biz.compcollege.comp.enums.CompTypeEnums;

@Component
public class CompCollegeTreeService {
	@Autowired
	GxCollegeMapper gxCollegeMapper;
	@Autowired
	BaseCollegeMapper baseCollegeMapper;
	@Autowired
	BizCompanyMapper bizCompanyMapper;
	@Autowired
	BizCompCollegeMapper bizCompCollegeMapper;
	
	private String getNull(String provId) {
		if(StringUtils.isBlank(provId)) {
			return null;
		}else {
			return provId;
		}
	}
	public List<CompCollegeBean> queryCompTree(BaseCollege baseCollege,String compId,CompTypeEnums compTypeEnums) {

		List<BaseCollege> dlist = gxCollegeMapper.queryRootCollegeProvinces(getNull(baseCollege.getProvinceId()), getNull(baseCollege.getCategoryId()), getNull(baseCollege.getLevel()));
		BizCompany bizCompany = bizCompanyMapper.selectByPrimaryKey(compId);
		
		List<CompCollegeBean> cRstList = new ArrayList<CompCollegeBean>();
		cRstList.add(getAllRoot());
		for(int i=0;i<dlist.size();i++) {
			BaseCollege baseRootCollege = dlist.get(i);
			
			List<BaseCollege> childList = gxCollegeMapper.queryCollege(getNull(baseCollege.getProvinceId()), baseRootCollege==null?null:baseRootCollege.getCategoryId(), getNull(baseCollege.getLevel()));
			
			CompCollegeBean rootNode = getRootCompCollegeBean(baseRootCollege);
			for(BaseCollege childBaseCollege : childList) {
				CompCollegeBean childNode = getCompCollegeBean(childBaseCollege, bizCompany, compId, compTypeEnums);
				childNode.setParentCategoryId(rootNode.getCategoryId());
				childNode.setCategoryId(childBaseCollege.hashCode()+"");
				rootNode.addComp(childNode);
			}
			List<CompCollegeBean> collegeList = rootNode.getChildList();
			/*
			 * 添加 选中不选中事件
			 */
			setCheckedCollegeColl(collegeList, compId);
			Collections.sort(collegeList, new Comparator<CompCollegeBean>() {

				@Override
				public int compare(CompCollegeBean o1, CompCollegeBean o2) {
					if(	o1.getCheckedStr()!=null &&"checked".equals(o1.getCheckedStr())) {
						return 1;
					}
					return 0;
				}
				
			});
			cRstList.add(rootNode);
		}
		
		for(CompCollegeBean compCollegeBean:cRstList) {
			setParentChecked(compCollegeBean,null);
		}
		return cRstList;
		
	}
	
	private void setParentChecked(CompCollegeBean compCollegeBean,CompCollegeBean parentCompCollegeBean) {
		if(compCollegeBean.getChildList()!=null && compCollegeBean.getChildList().size()>0) {
			for(CompCollegeBean childCompCollegeBean:compCollegeBean.getChildList()) {
				setParentChecked(childCompCollegeBean,compCollegeBean);
			}
		}else {
			if(parentCompCollegeBean!=null && "checked".equals(compCollegeBean.getCheckedStr())) {
				parentCompCollegeBean.setCheckedStr("checked");
			}
		}
	}
	
	private CompCollegeBean getAllRoot() {
		CompCollegeBean compCollegeBean = new CompCollegeBean();
		compCollegeBean.setCategoryId("1");
		compCollegeBean.setCategoryName("学校");
		compCollegeBean.setParentCategoryId("0");
		compCollegeBean.setIptId("1base");
		return compCollegeBean;
	}
	private CompCollegeBean getRootCompCollegeBean(BaseCollege baseRootCollege) {
		CompCollegeBean compCollegeBean = new CompCollegeBean();
		if(baseRootCollege==null || baseRootCollege.getCategoryId()==null) {
			compCollegeBean.setCategoryId("2");
			compCollegeBean.setCategoryName("未分类");
			compCollegeBean.setParentCategoryId("1");
		}else {
			compCollegeBean.setCategoryId(baseRootCollege.getCategoryId());
			compCollegeBean.setCategoryName(baseRootCollege.getCategoryName());
			compCollegeBean.setParentCategoryId("1");
		}
		compCollegeBean.setIptId(compCollegeBean.hashCode()+"base");
		return compCollegeBean;
	}
	
	private CompCollegeBean getCompCollegeBean(BaseCollege baseCollege,BizCompany bizCompany,String compId,CompTypeEnums compTypeEnums){
		CompCollegeBean compCollegeBean = new CompCollegeBean();
		compCollegeBean.setBaseCollege(baseCollege);
		compCollegeBean.setBizCompany(bizCompany);
		
		compCollegeBean.setProvinceId(baseCollege.getProvinceId());
		compCollegeBean.setProvinceName(baseCollege.getProvinceName());
		compCollegeBean.setIptId(baseCollege.getId());
		return compCollegeBean;
	}
	
	
	/**
	 * 	公司 对学校的权限查询
	 * @param compList
	 * @param collegeIdList
	 * @param compId
	 * @return
	 */
	public List<BizCompCollege> setCheckedCollegeColl(List<CompCollegeBean> compList,String compId) {
		List<String> collegeIdList = new ArrayList<>();
		for(CompCollegeBean compCollegeBean : compList) {
			collegeIdList.add(compCollegeBean.getBaseCollege().getId());
		}
		
		BizCompCollegeExample bizCompCollegeExample = new BizCompCollegeExample();
		BizCompCollegeExample.Criteria criteria = bizCompCollegeExample.createCriteria();
		
		criteria.andCollegeIdIn(collegeIdList);
		criteria.andCompanyIdEqualTo(compId);
		criteria.andTypeEqualTo(CompTypeEnums.compCollege.value);
		
		List<BizCompCollege> blist = bizCompCollegeMapper.selectByExample(bizCompCollegeExample);
		
		Map<String,BizCompCollege> dmap = new HashMap<String,BizCompCollege>();
		
		//公司对 学校的权限
		for(BizCompCollege bizCompCollege:blist) {
			dmap.put(bizCompCollege.getCollegeId(), bizCompCollege);
		}
		
		for(CompCollegeBean compCollegeBean : compList) {
			String colid = compCollegeBean.getBaseCollege().getId();
			BizCompCollege bizCompCollege = dmap.get(colid);
			if(bizCompCollege!=null) {
				compCollegeBean.setCheckedStr("checked");
			}else {
				compCollegeBean.setCheckedStr("");
			}
		}
		return blist;
	}
	
	
	
	/**
	 * 	学校 对公司的权限查询
	 * @param compList
	 * @param collegeIdList
	 * @param compId
	 * @return
	 */
	public List<BizCompCollege> setCheckedCompColl(List<CompCollegeBean> compList,String collegeId) {
		
		List<String> compIdList = new ArrayList<>();
		for(CompCollegeBean compCollegeBean : compList) {
			compIdList.add(compCollegeBean.getBizCompany().getId());
		}
		
		BizCompCollegeExample bizCompCollegeExample = new BizCompCollegeExample();
		BizCompCollegeExample.Criteria criteria = bizCompCollegeExample.createCriteria();
		
		criteria.andCollegeIdEqualTo(collegeId);
		criteria.andCompanyIdIn(compIdList);
		criteria.andTypeEqualTo(CompTypeEnums.collegeComp.value);
		
		List<BizCompCollege> blist = bizCompCollegeMapper.selectByExample(bizCompCollegeExample);
		
		Map<String,BizCompCollege> dmap = new HashMap<String,BizCompCollege>();
		
		//学校 对公司的权限查询
		for(BizCompCollege bizCompCollege:blist) {
			dmap.put(bizCompCollege.getCompanyId(), bizCompCollege);
		}
		
		for(CompCollegeBean compCollegeBean : compList) {
			String compIdD = compCollegeBean.getBizCompany().getId();
			BizCompCollege bizCompCollege = dmap.get(compIdD);
			if(bizCompCollege!=null) {
				compCollegeBean.setCheckedStr("checked");
			}else {
				compCollegeBean.setCheckedStr("");
			}
		}
		return blist;
	}
}
